package com.example.springboot.controller;

import com.example.springboot.entity.MerchantOrderReview;
import com.example.springboot.service.MerchantOrderReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/reviews")
public class MerchantOrderReviewController {
    @Autowired
    private MerchantOrderReviewService merchantOrderReviewService;

    // 根据订单ID获取评价详情
    @GetMapping("/byOrderId/{orderId}")
    public ResponseEntity<MerchantOrderReview> getReviewByOrderId(@PathVariable Integer orderId) {
        MerchantOrderReview review = merchantOrderReviewService.getReviewByOrderId(orderId);
        if (review == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(review);
    }

    // 添加评价
    @PostMapping("/add")
    public ResponseEntity<String> addReview(@RequestBody MerchantOrderReview review) {
        try {
            if (merchantOrderReviewService.getReviewByOrderId(review.getOrderId()) != null) {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("该订单已评价，无法重复提交");
            }
            if (review.getConfirmed() == null) {
                review.setConfirmed(false);
            } else {
                review.setConfirmed(review.getConfirmed() ? true : false);
            }
            if (review.getVersion() == null) {
                review.setVersion(1);
            }
            merchantOrderReviewService.addReview(review);
            return ResponseEntity.status(HttpStatus.CREATED).body("评价添加成功");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("评价添加失败: " + e.getMessage());
        }
    }

    // 更新评价
    @PutMapping("/update")
    public ResponseEntity<String> updateReview(@RequestBody MerchantOrderReview review) {
        try {
            MerchantOrderReview existingReview = merchantOrderReviewService.getReviewByOrderId(review.getOrderId());
            if (existingReview == null) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("评价不存在，无法更新");
            }
            merchantOrderReviewService.updateReview(review);
            return ResponseEntity.ok("评价更新成功");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("评价更新失败: " + e.getMessage());
        }
    }

    // 删除评价（修复）
    @DeleteMapping("/{reviewId}")
    public ResponseEntity<String> deleteReview(@PathVariable Integer reviewId) {
        try {
            MerchantOrderReview review = merchantOrderReviewService.getReviewByReviewId(reviewId);
            if (review == null) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("评价不存在，无法删除");
            }
            merchantOrderReviewService.deleteReview(reviewId);
            return ResponseEntity.ok("评价删除成功");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("评价删除失败: " + e.getMessage());
        }
    }

    // 确认评价（修复）
    @PutMapping("/confirm/{reviewId}")
    public ResponseEntity<String> confirmReview(@PathVariable Integer reviewId) {
        try {
            MerchantOrderReview review = merchantOrderReviewService.getReviewByReviewId(reviewId);
            if (review == null) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("评价不存在，无法确认");
            }
            if (review.getConfirmed()) {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("评价已确认，无需重复操作");
            }
            review.setConfirmed(true);
            merchantOrderReviewService.updateReview(review);
            return ResponseEntity.ok("评价确认成功");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("评价确认失败: " + e.getMessage());
        }
    }

    // 获取所有评价
    @GetMapping("/all")
    public ResponseEntity<List<MerchantOrderReview>> getAllReviews() {
        return ResponseEntity.ok(merchantOrderReviewService.getAllReviews());
    }

    // 根据商家ID获取评价列表
    @GetMapping("/byMerchantId/{merchantId}")
    public ResponseEntity<List<MerchantOrderReview>> getReviewsByMerchantId(@PathVariable Integer merchantId) {
        List<MerchantOrderReview> reviews = merchantOrderReviewService.getReviewsByMerchantId(merchantId);
        return ResponseEntity.ok(reviews);
    }

    // 根据用户ID获取评价列表
    @GetMapping("/byUserId/{userId}")
    public ResponseEntity<List<MerchantOrderReview>> getReviewsByUserId(@PathVariable Integer userId) {
        List<MerchantOrderReview> reviews = merchantOrderReviewService.getReviewsByUserId(userId);
        return ResponseEntity.ok(reviews);
    }
}